 package com.framework.base.dialect;

import org.apache.log4j.Logger;

import com.framework.base.taglib.page.Pagination;
import com.framework.base.util.StringUtil;

 
/**
 * @Copyright @ 2013
 * All right reserved
 * @version 创建时间：Created on 2013-2-28
 * @author 作者：Create by www.360buyli.com
 * @Email: 360buyli@gmail.com
 * @description eclipse注释模板
 */

public class OracleDialectImp  implements Dialect {
	private static Logger logger = Logger.getLogger(OracleDialectImp.class); // 作为日志的公用对象
	
	/**
	 * 用于组装分页查询条件
	 * @param paramsMap
	 * @param orderByFields
	 * @param orderType
	 * @throws Exception
	 */
	public String populatePageHQL(String queryString,String orderByFields,
			String orderType,Pagination page,String tableName) throws Exception {
		tableName=tableName.substring(0, tableName.length()-1);
		StringBuffer resultQueryString=new StringBuffer();
			resultQueryString.append(" (select rownum no,");
			resultQueryString.append(tableName);
			resultQueryString.append(".* from ");
			resultQueryString.append(tableName);
		if(queryString!=null&&queryString.length()>0 ){
			resultQueryString.append(" where ");
			resultQueryString.append(queryString);
		}
	
		resultQueryString.append(" order by ");
		if(!StringUtil.isNullorEmpty(orderByFields)&&!StringUtil.isNullorEmpty(orderType)){
			resultQueryString.append(orderByFields).append(" ").append(orderType);
		}else{
			resultQueryString.append(" datasortNumf desc");
		}
		resultQueryString.append(") ").append(tableName);
		resultQueryString.append(" where no>="+(page.getStart()+1)+" and no<"+(page.getEnd()+1)); //rownum排序号从1开始 所以在开始和结束时都加上1
		logger.info("oracle 分页查询:"+resultQueryString.toString());
		return resultQueryString.toString();
	}
	
	
	
	/**
	 * 用于分页时只查询记录数
	 * @param queryString
	 * @return
	 * @throws Exception
	 */
	public String populatePageHQL(String queryString,String tableName) throws Exception {
		StringBuffer resultQueryString=new StringBuffer();
		tableName=tableName.substring(0, tableName.length()-1);
		resultQueryString.append(" ");
		resultQueryString.append(tableName);
		if(queryString!=null&&queryString.length()>0 ){
			resultQueryString.append(" where ");
			resultQueryString.append(queryString);
		}
		logger.info("oracle 分页查询记录数:"+resultQueryString.toString());
		return resultQueryString.toString();
	}
	
	/**
	 * 用于组装分页查询条件(sqlserver)
	 * @param paramsMap
	 * @param orderByFields
	 * @param orderType
	 * @return
	 * @throws Exception
	 */
	public String populatePageHQL2(String queryString,String orderByFields,
			String orderType,Pagination page,String tableName) throws Exception {
		tableName=tableName.substring(0, tableName.length()-1);
		StringBuffer resultQueryString=new StringBuffer();
			resultQueryString.append(" (select top "+page.getRowsPerPage()+" ");
			resultQueryString.append(" * from ");
			resultQueryString.append(tableName);
			resultQueryString.append(" where idf not in ");
			resultQueryString.append(" (select top "+(page.getCurrentPage()-1)*page.getRowsPerPage()+" ");
			resultQueryString.append(" idf from "+tableName);
			if(queryString!=null&&queryString.length()>0 ){
				resultQueryString.append(" where ");
				resultQueryString.append(queryString);
			}
			resultQueryString.append(" ORDER BY  datasortNumf desc)");
		if(queryString!=null&&queryString.length()>0 ){
			resultQueryString.append(" and ");
			resultQueryString.append(queryString);
		}
	
		resultQueryString.append(" order by ");
		if(!StringUtil.isNullorEmpty(orderByFields)&&!StringUtil.isNullorEmpty(orderType)){
			resultQueryString.append(orderByFields).append(" ").append(orderType);
		}else{
			resultQueryString.append(" datasortNumf desc");
		}
		resultQueryString.append(") ").append(tableName);
		logger.info("sqlserver 分页查询:"+resultQueryString.toString());
		return resultQueryString.toString();
	}
	
	
	
	

}

